package cn.org.donald.service.impl;

import cn.org.donald.framework.pojo.entity.Table;
import cn.org.donald.framework.properties.TableProperties;
import cn.org.donald.framework.util.ListUtil;
import cn.org.donald.framework.util.StringUtil;
import cn.org.donald.mapper.TableMapper;
import cn.org.donald.service.game.TableService;
import org.apache.dubbo.config.annotation.Service;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;

/**
 * @author : Donald
 * @since : 2020/12/14 13:56
 */
@Service(version = "1.0.0",group = "donald")
public class TableServiceImpl implements TableService {

    private static Logger logger = LoggerFactory.getLogger(TableServiceImpl.class);

    @Autowired
    private TableMapper tableMapper;

    @Autowired
    private TableProperties tableProperties;

    @Override
    public boolean isExistByTableName(String tableName) {
        List<Table> tables = tableMapper.selectTableName();
        return !ListUtil.isEmptyOrHasClassFieldAllEmpty(tables);
    }

    @Override
    public int cleanTable() {
        List<Table> tables = tableMapper.selectTableName();
        logger.info("Starting clean tables!");
        return tables.stream().mapToInt(table -> {
            if (StringUtil.isEmpty(tableProperties.getExceptTable()) && tableProperties.getExceptTable().contains(table.getTableName())){
                return 0;
            }
            logger.info("cleaning table is {}",table);
            return tableMapper.deletePhysical(table.getTableName(),tableProperties.getDay());
        }).sum();
    }
}
